package common;

public class Piece {

    /**
     * First value.
     */
    private byte value1;
    /**
     * Second value.
     */
    private byte value2;

    /**
     * Creates a new piece with the given values.
     *
     * @param value1.
     * @param value2.
     */
    public Piece(byte value1, byte value2) {
        this.value1 = value1;
        this.value2 = value2;
    }

    /**
     * Return true if the piece has the given value and false, otherwise.
     */
    public boolean hasValue(byte value) {
        return value1 == value || value2 == value;
    }

    /**
     * Returns the opposite value of the piece.
     */
    public byte getOppositeValue(byte value) {
        if (value1 == value) {
            return value2;
        } else if (value2 == value) {
            return value1;
        }
        return -1;
    }

    /**
     * Returns the sum of the two values.
     */
    public byte getValuesSum() {
        return (byte) (value1 + value2);
    }

    /**
     * Return true if the piece is a double and false, otherwise.
     */
    public boolean isDouble() {
        return value1 == value2;
    }

    /**
     * Returns the first value.
     */
    public byte getValue1() {
        return value1;
    }

    /**
     * Returns the second value.
     */
    public byte getValue2() {
        return value2;
    }

    @Override
    public int hashCode() {
        int hash = 7;
        hash = 53 * hash + this.value1;
        hash = 53 * hash + this.value2;
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Piece other = (Piece) obj;
        if (this.value1 != other.value1) {
            return false;
        }
        if (this.value2 != other.value2) {
            return false;
        }
        return true;
    }
}
